<?php
namespace app\system\validate;

use think\Validate;

class User extends Validate
{

    protected $rule = [
        'name' => 'require',
        'tel' => 'require|checkTel|unique:user',
        // 'user_type' => 'checkType',
        'password' => 'require',
    ];

    protected $message = [
        'name.require' => '请填写姓名',
        'tel.require' => '请填写手机号',
        'tel.unique' => '手机号已存在',
        'password.require' => '请填写密码',
    ];

    protected $scene = [
        'add' => ['user_type','name','tel'],
        'edit' => ['user_type','name','tel'],
    ];

    /**
     * 验证手机格式
     * @param $value
     * @return bool|string
     */
    protected function checkTel($value)
    {
        if (checkTel($value)) {
            return true;
        } else {
            return '手机号格式错误';
        }
    }

    /**
     * 验证用户类型
     * @param $value
     * @param $rule
     * @param $data
     * @return bool|string
     */
    protected function checkType($value, $rule, $data)
    {
        if (in_array($value, [0,1,2,3])) {
            if ($value == 3 && $data['pid'] <= 0) {
                return '请选择上级店长';
            } else {
                return true;
            }
        } else {
            return '请选择用户类型';
        }
    }
}


